home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / ALLTRIM.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  1KB  |  56 lines

  1. /*********
  2. *
  3. * ALLTRIM.C
  4. *
  5. * by Tom Rettig
  6. *
  7. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  8. *
  9. *  Syntax: ALLTRIM( <expC> )
  10. *  Return: <expC> with leading and trailing spaces removed.
  11. *
  12. *********/
  13. #include "trlib.h"
  14.  
  15. TRTYPE alltrim()
  16. {
  17.    static char funcname[] = { "alltrim" };
  18.    char *instr, *ret;
  19.    int i, j, k, len;
  20.  
  21.    if ( PCOUNT==1 && ISCHAR(1) )
  22.    {
  23.       instr  = _parc(1);
  24.       len    = _tr_strlen(instr);
  25.  
  26.       if ((instr[len-1]==SPACEC || instr[0]==SPACEC) )
  27.       {
  28.          ret = _tr_allocmem( (unsigned)(len+1) );
  29.          if ( ret )
  30.          {
  31.             for ( i=0; instr[i]==SPACEC; i++ )
  32.                ;                                 /* first char on left */
  33.  
  34.             for ( j=len-1; instr[j]==SPACEC; j-- )
  35.                ;                                 /* last char on right */
  36.  
  37.             /* fill return buffer with chars */
  38.             for ( k=0; i<=j; i++, k++ )
  39.                ret[k] = instr[i];
  40.  
  41.             ret[k] = NULLC;
  42.             _retc( ret );
  43.             _tr_freemem( ret ,(unsigned)(len+1));
  44.          }
  45.          else
  46.             _retc( _tr_errmsgs(funcname,E_ALLOC) );
  47.       }
  48.       else
  49.          /* nothing to trim, return original string */
  50.          _retc( instr );
  51.    }
  52.    else
  53.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  54. }
  55. /* eof */
  56.